package com.gjy.sql.util;

import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.RecursiveAction;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-03-05 17:10:15
 */
public class OracleBatchOperator extends RecursiveAction {
    private static final Logger log = LoggerFactory.getLogger(OracleBatchOperator.class);

    private static final int THRESHOLD = 10000;

    private final List<String> list;

    public OracleBatchOperator(List<String> list) {
        this.list = list;
    }

    @Override
    protected void compute() {
        boolean flag = list.size() < THRESHOLD;
        if (flag) {
            /*try (Connection connection = OracleUtil.connection();
                 Statement statement = connection.createStatement()) {
                for (String sql : list) {
                    statement.addBatch(sql);
                }
                statement.executeBatch();
                statement.clearBatch();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }*/
            log.info("{}", list.size());
        } else {
            List<List<String>> partition = Lists.partition(list, list.size() / 2);
            OracleBatchOperator r1 = new OracleBatchOperator(partition.get(0));
            OracleBatchOperator r2 = new OracleBatchOperator(partition.get(1));
            invokeAll(r1, r2);
        }
    }
}
